import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

# 基类
class Base():
    # 初始化驱动
    def __init__(self,driver):
        self.driver=driver
    # 封装打开浏览器方法
    def open_url(self,url):
        self.driver.get(url)
    # 封装退出浏览器方法
    def quit(self):
        self.driver.quit()
    # 封装时间戳
    def time_str(self):
        return time.strftime("%Y%m%d%H%M%S",time.localtime())
    # 封装查找定位元素方法
    def find_element(self,loc):
        # 异常
        try:
            # 显式等待
            WebDriverWait(self.driver,20,0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            # 截图
            self.driver.save_screenshot(self.time_str()+'.png')
            print(e)

    # 封装输入文本方法
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)
    # 封装点击操作方法
    def click(self,loc):
        self.find_element(loc).click()
    # 封装文本方法，用于断言
    def text(self,loc):
        return self.find_element(loc).text
    # 封装清除方法
    def clear(self,loc):
        self.find_element(loc).clear()
    # 4、滚动条（执行js代码）
        # 滚动任意像素
    def scrollTo(self,x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')

        # 滚动到底部
    def scrollHeight(self):
        self.driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')

    # 弹框处理
    def alert_accept(self):
        self.driver.switch_to.alert.accept()

    def alert_dismiss(self):
        self.driver.switch_to.alert.dismiss()

    # iframe处理
        # 进入frame框架
    def frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))

        # 退出frame框架
    def frame_quit(self):
        self.driver.switch_to.default_content()

    # 切换页面
    def handles(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    # 鼠标事件
        # 鼠标双击
    def action_double(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).perform()

        # 鼠标右击
    def action_context(self,loc):
        ActionChains(self.driver).context_click(self.find_element(loc)).perform()

        # 鼠标拖拽
    def action_move(self,loc):
        ActionChains(self.driver).move_to_element(self.find_element(loc)).perform()

    # 键盘事件
        # 回车事件
    def keys_enter(self,loc):
        self.driver.find_element(loc).send_keys(Keys.ENTER)
        # 剪切
    def keys_ctrl_x(self,loc):
        self.driver.find_element(loc).send_keys(Keys.CONTROL,'x')
        # 复制
    def keys_ctrl_c(self,loc):
        self.driver.find_element(loc).send_keys(Keys.CONTROL,'c')
        # 粘贴
    def keys_ctrl_v(self,loc):
        self.driver.find_element(loc).send_keys(Keys.CONTROL,'v')

    # 浏览器事件
        # 返回
    def back(self):
        self.driver.back()
        # 关闭
    def close(self):
        self.driver.close()
        # 刷新
    def refresh(self):
        self.driver.refresh()


    # 下拉框选择
    def select_index(self,loc,info):
        Select(self.find_element(loc)).select_by_index(info)

    def select_vslue(self,loc,info):
        Select(self.find_element(loc)).select_by_value(info)

    def select_text(self,loc,info):
        Select(self.find_element(loc)).select_by_visible_text(info)

    # （用不上的技术点，请封装正确）